'use strict';

module.exports = app => {
  const { STRING, INTEGER, ENUM, BOOLEAN } = app.Sequelize;

  const Tag = app.model.define('tag', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: STRING(50),
      allowNull: false,
      unique: true,
      comment: '标签名称',
    },
    type: {
      type: ENUM('color', 'style', 'occasion', 'season', 'material', 'brand', 'custom'),
      allowNull: false,
      comment: '标签类型',
    },
    color: {
      type: STRING(7),
      allowNull: true,
      comment: '标签颜色',
    },
    description: {
      type: STRING(200),
      allowNull: true,
      comment: '标签描述',
    },
    sort_order: {
      type: INTEGER,
      defaultValue: 0,
      comment: '排序',
    },
    is_system: {
      type: BOOLEAN,
      defaultValue: false,
      comment: '是否系统标签',
    },
    is_active: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '是否启用',
    },
    // 时间戳字段由Sequelize自动管理
  }, {
    tableName: 'tags',
    comment: '标签表',
    indexes: [
      {
        fields: ['type'],
      },
      {
        fields: ['is_system'],
      },
      {
        fields: ['is_active'],
      },
    ],
  });

  Tag.associate = function() {
    // 标签可以关联多个衣服
    app.model.Tag.belongsToMany(app.model.Clothing, {
      through: app.model.ClothingTag,
      foreignKey: 'tag_id',
      otherKey: 'clothing_id',
      as: 'clothes',
    });
  };

  return Tag;
};
